package cs2340.quarterblack.wheresmystuff;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Signup extends Activity {
	
	Context context;
	Button btnSignup;
	EditText etAccount;
	EditText etPassword;
	EditText etEmail;
	TextView tvFailedLogin;
	
	Account account;
	int failedAttempts;
	boolean loginSuccessful;

	/**
	 * creates the sign up page
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);
		
		btnSignup = (Button)findViewById(R.id.ci_signup_btn_signup);
		
		etAccount = (EditText)findViewById(R.id.ci_signup_userName);
		etPassword = (EditText)findViewById(R.id.ci_signup_password);
		etEmail = (EditText)findViewById(R.id.ci_signup_email);
		failedAttempts = 0;
		/**
		 * listens for the Sign up button press and then attempts to create a new account
		 */
		btnSignup.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// account and password entered
				String stAccount = etAccount.getText().toString();
				String stPassword = etPassword.getText().toString();
				String stEmail = etEmail.getText().toString();
				account = new Account(stAccount,stPassword);
				account.addEmail(stEmail);
				loginSuccessful = account.addUser();
				if (loginSuccessful){
					if(stAccount != null && stPassword !=null && stEmail !=null)
					{
						Toast.makeText(getApplicationContext(), "Account was succesfully created", 5).show();
						Intent signin= new Intent(Signup.this,LoginPage.class);
						signin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						Signup.this.startActivity(signin);
					}
					else
					{
						Toast.makeText(getApplicationContext(),"Invalid information",5).show();
					}
				}
				else {
					Toast.makeText(getApplicationContext(), "Account already exists", 5).show();
				}
			}
		});
	}
	/**
	 * called by the system to inflate the menu for the page
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

}
